using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;


namespace RUPS
{
    public partial class A_ComandaMateriale : Form
    {
        public int nrO;
        public DateTime dataO;
        public int idFzO;
        public int idMatO;
        public int cantO;
        public bool statusO;
        SqlConnection conexiune;
        DataSet dataset;
        SqlDataAdapter adaptorMat;
        SqlDataAdapter adaptorFz;
             
        public A_ComandaMateriale()
        {
            InitializeComponent();
        }

        private void Comanda_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'comenziMaterialeDataSet.comenziMateriale' table. You can move, or remove it, as needed.
            try
            {
            this.comenziMaterialeTableAdapter2.Fill(this.comenziMaterialeDS.comenziMateriale);
            //SqlCommand comanda = new SqlCommand("select max(id), id  from comenziMateriale", comenziMaterialeTableAdapter.Connection);
            //SqlDataReader reader;
           conexiune = new SqlConnection(comenziMaterialeTableAdapter.Connection.ConnectionString);
            conexiune.Open();
            SqlCommand comanda = new SqlCommand("maximIdcomenziMateriale", conexiune);
                SqlParameter id;
                id = new SqlParameter("@maxId", SqlDbType.Int);
                id.Direction = ParameterDirection.Output;
                 comanda.Parameters.Add(id);
            comanda.CommandType = CommandType.StoredProcedure;
          //  reader = comanda.ExecuteReader();
            int rez;

            comanda.ExecuteNonQuery();
            if (comanda.Parameters[0].Value.ToString() == "")
                rez = 1;
            else
                rez = Convert.ToInt32(comanda.Parameters[0].Value);

         
            this.nr.Text = rez.ToString();
            adaptorMat = new SqlDataAdapter("select id, numematerial from  tipmaterial", comenziMaterialeTableAdapter.Connection);
            dataset = new DataSet();
            adaptorMat.Fill(dataset,"materiale");
            this.a_material.DataSource = dataset.Tables["materiale"];
            this.a_material.DisplayMember = dataset.Tables["materiale"].Columns["numematerial"].ToString();
            this.a_material.ValueMember = dataset.Tables["materiale"].Columns["id"].ToString();
            a_material.Text = "";

         
            adaptorFz = new SqlDataAdapter("select id, denumire from furnizori" ,comenziMaterialeTableAdapter.Connection);
            adaptorFz.Fill(dataset,"furnizori");
            this.a_furnizor.DataSource = dataset.Tables["furnizori"];
            this.a_furnizor.DisplayMember = dataset.Tables["furnizori"].Columns["denumire"].ToString();
            this.a_furnizor.ValueMember = dataset.Tables["furnizori"].Columns["id"].ToString();
            a_furnizor.Text = "";

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {

                if (a_dataComanda.Value.ToString() == "")
                    throw new InvalidOperationException("nu ati completat datele obligatorii");
                if (a_furnizor.Text== "")
                    throw new InvalidOperationException("nu ati completat datele obligatorii");

                if (a_material.Text == "")
                    throw new InvalidOperationException("nu ati completat datele obligatorii");
                if (a_cantitate.Text == "")
                    throw new InvalidOperationException("nu ati completat datele obligatorii");

                comenziMaterialeTableAdapter.Insert(a_dataComanda.Value, int.Parse(a_furnizor.SelectedValue.ToString()), int.Parse(a_cantitate.Text), int.Parse(a_material.SelectedValue.ToString()), false);
                gridComenziMateriale.DataSource = null;
                gridComenziMateriale.Rows.Clear();
                SqlDataAdapter da = new SqlDataAdapter("select * from comenziMateriale", comenziMaterialeTableAdapter.Connection);
                DataSet ds = new DataSet();
                da.Fill(ds, "comenziMat");

                gridComenziMateriale.DataSource = ds.Tables["comenziMat"];
            }
            catch (InvalidOperationException inv)
            {
                MessageBox.Show("atentie", inv.Message, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            catch (SqlException err)
            {
                MessageBox.Show(err.Message);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void revoca_Click(object sender, EventArgs e)
        {
            this.a_cantitate.Text = "";

           SqlConnection  conexiune = new SqlConnection(comenziMaterialeTableAdapter.Connection.ConnectionString);
            conexiune.Open();
            SqlCommand comanda = new SqlCommand("maximIdcomenziMateriale", conexiune);
            SqlParameter id;
            id = new SqlParameter("@maxId", SqlDbType.Int);
            id.Direction = ParameterDirection.Output;
            comanda.Parameters.Add(id);
            comanda.CommandType = CommandType.StoredProcedure;
            int rez;

            comanda.ExecuteNonQuery();
            if (comanda.Parameters[0].Value.ToString() == "")
                rez = 1;
            else
                rez = Convert.ToInt32(comanda.Parameters[0].Value);


            this.nr.Text = rez.ToString();
            status.Checked = false;
            status.Enabled = false;
            a_furnizor.Text = "";
            a_material.Text = "";
        }

        private void gridComenziMateriale_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                DataGridViewRow rand = gridComenziMateriale.SelectedRows[0];
                cantO = Int32.Parse(rand.Cells[3].Value.ToString());
                this.a_cantitate.Text = cantO.ToString();

                dataO = DateTime.Parse(rand.Cells[1].Value.ToString());
                this.a_dataComanda.Value = dataO;

                SqlCommand comanda = new SqlCommand("denumireFurnizor", conexiune);
                SqlParameter idFz, denFz;
                idFz = new SqlParameter("@Id", SqlDbType.Int);
                idFz.Direction = ParameterDirection.Input;
                idFzO = int.Parse(rand.Cells[2].Value.ToString());
                idFz.Value = idFzO;
                comanda.Parameters.Add(idFz);

                denFz = new SqlParameter("@nume", SqlDbType.VarChar);
                denFz.Direction = ParameterDirection.Output;
                denFz.Size = 50;
                comanda.Parameters.Add(denFz);

                comanda.CommandType = CommandType.StoredProcedure;
                comanda.ExecuteNonQuery();
                string denumire = comanda.Parameters[1].Value.ToString();
                //int index = this.a_furnizor.Items.IndexOf(denumire);
                int  index = a_furnizor.FindStringExact(denumire);
                this.a_furnizor.SelectedIndex = index;
                this.a_furnizor.SelectedText = denumire;
                this.a_furnizor.SelectedValue = idFzO;



                comanda = new SqlCommand("denumireTipMaterial", conexiune);
                SqlParameter idMat, denMat;
                idMat = new SqlParameter("@id", SqlDbType.Int);
                idMat.Direction = ParameterDirection.Input;
                idMatO = int.Parse(rand.Cells[4].Value.ToString());
                idMat.Value = idMatO;
                comanda.Parameters.Add(idMat);

                denMat = new SqlParameter("@nume", SqlDbType.VarChar);
                denMat.Size = 50;
                denMat.Direction = ParameterDirection.Output;
                comanda.Parameters.Add(denMat);

                comanda.CommandType = CommandType.StoredProcedure;
                comanda.ExecuteNonQuery();

                string denumireMaterial = comanda.Parameters[1].Value.ToString();
                int idxMat = a_material.FindStringExact(denumireMaterial);
                a_material.SelectedIndex = idxMat;
                a_material.SelectedText = denumireMaterial;
                a_material.SelectedValue = idMatO;

                status.Enabled = true;
                status.Checked = bool.Parse(rand.Cells[5].Value.ToString());

                statusO = bool.Parse(rand.Cells[5].Value.ToString());
                nrO = int.Parse(rand.Cells[0].Value.ToString());

                this.nr.Text = nrO.ToString();
               
               
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

             


        }

        private void a_cantitate_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == 8 || e.KeyChar == ',')
            {
                base.OnKeyPress(e);
            }
            else
                e.Handled = true;
        }

        private void sterge_Click(object sender, EventArgs e)
        {
            try
            {

                if (a_dataComanda.Value.ToString() == "")
                    throw new InvalidOperationException("nu ati completat datele obligatorii");
                if (a_furnizor.SelectedValue.ToString() == "")
                    throw new InvalidOperationException("nu ati completat datele obligatorii");

                if (a_material.Text == "")
                    throw new InvalidOperationException("nu ati completat datele obligatorii");
                if (a_cantitate.Text == "")
                    throw new InvalidOperationException("nu ati completat datele obligatorii");


                conexiune = comenziMaterialeTableAdapter1.Connection;
                conexiune.Open();
                SqlCommand comanda = new SqlCommand("delete from ComenziMateriale where id = " + nrO, conexiune);
                comanda.ExecuteNonQuery();

                comenziMaterialeBindingSource1.ResetBindings(false);
                comenziMaterialeDataSet.comenziMaterialeDataTable td = new comenziMaterialeDataSet.comenziMaterialeDataTable();

                comenziMaterialeDataSetTableAdapters.comenziMaterialeTableAdapter dta = new RUPS.comenziMaterialeDataSetTableAdapters.comenziMaterialeTableAdapter();

                gridComenziMateriale.DataSource = null;

                dta.Fill(td);

                comenziMaterialeBindingSource1.DataSource = td;
                gridComenziMateriale.DataSource = comenziMaterialeBindingSource1;
                gridComenziMateriale.Invalidate();
                conexiune.Close();
            }
            catch (InvalidOperationException inv)
            {
                MessageBox.Show(inv.Message, "atentie", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "atentie", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                if (a_dataComanda.Value.ToString() == "")
                    throw new InvalidOperationException("nu ati completat datele obligatorii");
                if (a_furnizor.Text == "")
                    throw new InvalidOperationException("nu ati completat datele obligatorii");

                if (a_material.Text == "")
                    throw new InvalidOperationException("nu ati completat datele obligatorii");
                if (a_cantitate.Text == "")
                    throw new InvalidOperationException("nu ati completat datele obligatorii");



                comenziMaterialeDSTableAdapters.comenziMaterialeTableAdapter dat = new RUPS.comenziMaterialeDSTableAdapters.comenziMaterialeTableAdapter();
                comenziMaterialeDS.comenziMaterialeDataTable tb = new comenziMaterialeDS.comenziMaterialeDataTable();

                DateTime dataNoua = DateTime.Parse(a_dataComanda.Value.ToString());

                int idFz = int.Parse(a_furnizor.SelectedValue.ToString());
                int idMat = int.Parse(a_material.SelectedValue.ToString());

                comenziMaterialeTableAdapter.UpdateComenziMateriale(dataNoua, idFz, 
                    int.Parse(a_cantitate.Text), idMat, status.Checked,
                    nrO, dataO, idFzO, cantO, idMatO, statusO);

                gridComenziMateriale.DataSource = null;
                comenziMaterialeBindingSource1.ResetBindings(false);
                dat.Fill(tb);

                comenziMaterialeBindingSource1.DataSource = tb;
                gridComenziMateriale.DataSource = comenziMaterialeBindingSource1;
                gridComenziMateriale.Invalidate();

                status.Enabled = false;

            }
            catch (InvalidOperationException inv)
            {
                MessageBox.Show(inv.Message, "atentie", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }




        }

       
        
       
        
    }
}